<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Permission extends MY_Controller {
	
	public function __construct()
	{
		parent::__construct();
		$this->load->model('permission_model');	
	}
	
	public function index()
	{
		$data['list'] = $this->get_info();
		$this->load->view('admin/permission/main',$data);
	}
	
	function get_info($id=NULL){
		if(!$id)
		{
			return $data['list'] = $this->permission_model->find_all();
		}
		if($id)
		{
			//sleep(1);
			return $data['list']  = $this->permission_model->find($id);	
		}
		die(json_encode($data['list']));
	}
	
	function form($id=NULL)
	{
		//sleep(1);
		$data='';
		$data['perm']='';
		
		if(!empty($id))
			$data['perm'] = $this->permission_model->find($id);
		
		$this->load->view('admin/permission/form', $data);
	}
	
		
	function create($form)
	{
		if($this->permission_model->insert($form))
			{
				$data['error']= false;
				$data['msg']  = '<span style="color:green">Success..<br>Thanks You !</span>';
			}
			else{
				$data['error']= true;
				$data['msg']  = 'Data Base Error <br> ERROR: per/ins/100';
			}
			
		return $data;
	}
	
		
	function edit($id,$form)
	{
		if($this->permission_model->update($id, $form)){
			$data['error']= false;
			$data['msg']= ' Successfully Updated..';
		}
		else{
			$data['error']= true;
			$data['msg']  = 'Data Base Error <br> ERROR: per/upd/101';
		}
			
		return $data;
	}
	
	function delete($id=NULL)
	{
		if (!empty($id))
		{	
			//check if the permission has no attached role in role_permission table
			
				if ($this->permission_model->delete($id))
				{
					//success
						$data['msg']	= ' Successfully Deleted..';
						$data['error']	= false;
					}
				else{
					//failed to delete
					$data['error']	= true;
					$data['msg']	= ' Failed to Delete..<br>Check if the permission(s) are connected to one or more role';
				}
		die(json_encode($data));
		}
		
	}
	
	function save()
	{
		$this->form_validation->set_message('is_unique', "The<b> %s </b>already Exist!");
		$this->form_validation->set_message('is_unique2', "The<b> %s </b>already Exist!");
		
		//check if it new record or update to set the validation rules
		$id = $this->input->post('id');
		
		$this->form_validation->set_rules('controller'	, 'Controller'	, 'trim|required|min_length[3]');
		$this->form_validation->set_rules('function'	, 'Function'	, 'trim|required|min_length[3]');
		$this->form_validation->set_rules('alias_name'	, 'Alias Name'	, 'trim|required');
		$this->form_validation->set_rules('description'	, 'Description'	, 'trim|required');
		
				
		if ($this->form_validation->run() == FALSE)
		{
			$data['error']= true;
			$data['msg']  = $this->form_validation->getErrorsArray();
			
			die(json_encode($data));

			//die(json_encode($errors));
		}
		else
		{
			//ucwords(strtolower($bar));
			$form['controller'] 	= strtolower($this->input->post('controller'));
			$form['function'] 		= strtolower($this->input->post('function'));
			$form['alias_name'] 	= $this->input->post('alias_name');
			$form['description'] 	= $this->input->post('description');

			
			//NEW (insert)
			if($this->input->post('id')=='')
			{
				$data = $this->create($form);

			}
			
			//Edit (update)
			if($this->input->post('id'))
			{
				$id = $this->input->post('id');
				$data = $this->edit($id, $form);
			}
			
			die(json_encode($data));
		}

	}
}
